#include<p24fj128ga006.h>
#include <libpic30.h>
#include "process_data.h"


/*******************************************************
 Data processing UART 
 <02><from><to><type><len><Pload><03>
 <From>  - 00 -> PC
           01 -> Device
 <to>    - 00 -> PC
           01 -> Device
 <type>  - 00 -> x
           01 -> x
           02 -> Cmd
           03 -> Reply
           0A -> Ack
           0B -> Nack
 <len>  -  data length
 <Pload>-  <type of data><data>
              00 -> x
              01 -> Get Mac Addr
              02 -> Get Grants list
              03 -> Get Report list
                         <data>
                           00 -> Onu1
                           01 -> Onu2
                           0N -> Onu N 
                           FF -> All 
              04 -> Get counter32 value
              05 -> Put Q list(ONU only)
              06 -> Get Q list(OLT only)
              07 -> x
              08 -> x
              09 -> x
             
 <03>
*******************************************************/
void process_URX1(void)//input from feeder - serial port
{
  // int i;

   if(URX1_Ready == 0x01)
   {       
      if((in1_buffer[1]==0x00)&&(in1_buffer[2]==0x01))
      {
          Reply_URX1();
      }   
       URX1_Ready = 0x00;
   }
   
}

/***************************************************************************************

****************************************************************************************/
void process_URX2(void)//input from optical module - input from fiber
{
   int i,j;
   //unsigned char temp[50];

   if(URX2_Ready == 0x01)
   {
       //check llid if equal to system send back onu only
       //.....
       //.....
       //other
       //U1TXREG = 0x85;//debug

       if((in2_buffer[9] ==  OLT_MACAddr[0])&& (in2_buffer[10] ==  OLT_MACAddr[1])&&(in2_buffer[11] ==  OLT_MACAddr[2])&&(in2_buffer[12] ==  OLT_MACAddr[3])&&(in2_buffer[13] ==  OLT_MACAddr[4])&&(in2_buffer[14] == OLT_MACAddr[5]))//check DA
       {
           //check length/type
           if((in2_buffer[21] ==  0x88)&& (in2_buffer[22] ==  0x08))//88-08 - MPCP
           {
               // U1TXREG = 0x86;
                //Check opcode
                //if((in2_buffer[23] == 0x00) && (in2_buffer[24] == 0x01))//Pulse
                //{                
                //}
                //if((in2_buffer[23] == 0x00) && (in2_buffer[24] == 0x02))//GATE
                //{                
                //}
                if((in2_buffer[23] == 0x00) && (in2_buffer[24] == 0x03))//Report
                {    
                   //U1TXREG = 0x42;//debug               
                   if(in2_buffer[29] == 0x00)//report with 0 no of q set
                   {
                      //U1TXREG = 0x87;//debug
                      in2_buffer[0]= 0x00;// discard frame
                      goto exit;
                   }
                   else
                   {
                      //send to DBA agent controller check mac and assign no
                      for(i=0;i<20;i++)//check onu mac
                      {                       
                         if((in2_buffer[15] ==  MACAddrTable[i][0])&&(in2_buffer[16] ==  MACAddrTable[i][1])&&(in2_buffer[17] ==  MACAddrTable[i][2])&&(in2_buffer[18] ==  MACAddrTable[i][3])&&(in2_buffer[19] ==  MACAddrTable[i][4])&&(in2_buffer[20] ==  MACAddrTable[i][5]))
                         {
                            //temp[2] = i;//grand list map
                            for(j=0;j<50;j++)
                            {
                               report_list[i][j+1] = in2_buffer[29+j];
                            }  
                            report_list[i][0] = 0x01;//ready
                            //report_list[i][1] = 42;//len
                            break;
                         }                                            
                      }                     
                   }

                }
                //else if((in2_buffer[23] == 0x00) && (in2_buffer[24] == 0x04))//Reg_req
                //{
                  //send to Discovery agent controller    
                //}
                //else if((in2_buffer[23] == 0x00) && (in2_buffer[24] == 0x05))//Reg
                //{
                   //send to Discovery agent controller     
                //}
                //else if((in2_buffer[23] == 0x00) && (in2_buffer[24] == 0x06))//Reg_ack
                //{
                   //send to Discovery agent controller     
                //}
                else//opcode not supported
                {
                  in2_buffer[0]= 0x00;// discard frame
                  //exit(1);// break;
                }
exit:
                //send copy to utx1
                for(i=0;i<0x49;i++)
                {
                    U1TXREG = in2_buffer[i];      
                    __delay32(1500); 
                }
                time_stamp(); 
                in2_buffer[0] = 0x00;
           }
           else//data with length
           {
              for(i=0;i<in2_buffer[5];i++)//length/type != mac client pass to mac client
              {
                 U1TXREG = in2_buffer[i];      
                 __delay32(1500); 
              }
           }
       }
       else//send to feeder 
       {
           for(i=0;i<0x49;i++)//pass to mac client
           {
              U1TXREG = in2_buffer[i];      
              __delay32(1500); 
           }

       }
    //U2TXREG = 0x88;//debug
    URX2_Ready = 0x00;   
   }
   if(URX2_Ready == 0x02)//receive data 
   {
       for(i=0;i<length_type;i++)//send to feeder utx1
       {
          U1TXREG = in2_buffer[i];      
          __delay32(1500);
       }
       URX2_Ready = 0x00;
   }
}



/********************************************************************************

Reply data -> to PC (UART1)

*********************************************************************************/

void Reply_URX1(void)
{
   int i,j,M;
   unsigned char N;
   unsigned char *temp_store;


   switch(in1_buffer[5])
   {      
       case 0x01://reply mac addr
              in1_buffer[0] = 0x02; //start of data 
              in1_buffer[1] = 0x01; //from device
              in1_buffer[2] = 0x00; //to PC
              in1_buffer[3] = 0x03; //reply
              
              in1_buffer[5] = 0x01; //get MAC addr
              for(i=0;i<6;i++)
              {
                in1_buffer[i+6] = OLT_MACAddr[i]; //get MAC addr
              }
              in1_buffer[4] = i+1; //length 
              in1_buffer[i+6] = 0x03; //end of data
         break;
       case 0x02://reply grants list only at onu
              in1_buffer[0] = 0x02; //start of data 
              in1_buffer[1] = 0x01; //from device
              in1_buffer[2] = 0x00; //to PC
              in1_buffer[3] = 0x0B; //(NACK)
              in1_buffer[4] = 0x01; //length
              in1_buffer[5] = in1_buffer[5]; //                         
              in1_buffer[6] = 0x03; //end of data
         break;
       case 0x03://reply report list
              in1_buffer[0] = 0x02; //start of data 
              in1_buffer[1] = 0x01; //from device
              in1_buffer[2] = 0x00; //to PC
              in1_buffer[3] = 0x03; //reply             
              in1_buffer[5] = 0x03; //reply report list
              N = in1_buffer[6];
              
              if(N< 22)
              {
                  if(N == 21)
                  {
                     M = 5;
                     in1_buffer[4] = (20*M)+1; //length(20*M)+1
                     for(j=0;j<M;j++)//load data to buffer
                     {
                        for(i=0;i<(M*20);i++)
                        {
                           in1_buffer[i+6] = report_list[j][i]; //report list
                        }                  
                     }
                     in1_buffer[(M*20)+6] = 0x03; //end of data
                  } 
                  else if(N < 21)
                  {                    
                     in1_buffer[4] = 21; //lengthi+1
                     for(i=0;i<20;i++)
                     {
                        in1_buffer[i+6] = report_list[N][i]; //report list
                     }
                     in1_buffer[i+6] = 0x03; //end of data

                  }                 
                  
              }
              else //out of scope
              {
                  M = 1;
                  in1_buffer[4] = 0x01; //lengthi+1
                  in1_buffer[3] = 0x0B; //change to not ACK 
                  in1_buffer[6] = 0x03; //end of data
              }
              
              
              
              
         break;
       case 0x04://get counter32 value
              in1_buffer[0] = 0x02; //start of data 
              in1_buffer[1] = 0x01; //from device
              in1_buffer[2] = 0x00; //to PC
              in1_buffer[3] = 0x03; //reply
              
              in1_buffer[5] = 0x01; //get counter32 value

              temp_store = changeLong2Byte(timer_counter32); 
              for(i=0;i<4;i++)
              {
                in1_buffer[i+6] = temp_store[i]; //get MAC addr
              }
              in1_buffer[4] = i+1; //length
              in1_buffer[i+6] = 0x03; //end of data
         break;       
       case 0x00:case 0x05:case 0x06:case 0x07:case 0x08:case 0x09:case 0x0A:case 0x0B: //not used 
              in1_buffer[0] = 0x02; //start of data 
              in1_buffer[1] = 0x01; //from device
              in1_buffer[2] = 0x00; //to PC
              in1_buffer[3] = 0x0B; //reply
              in1_buffer[4] = 0x01; //length
              in1_buffer[5] = in1_buffer[5]; //reply grants list(NACK)                          
              in1_buffer[6] = 0x03; //end of data
         break;   
         //goto exit;      
       
   }
   //send to PC
   send_URX1();
//exit:
//  Nop();
  
}

void send_URX1(void)
{
    int i;
 
    for(i=0;i<in1_buffer[4]+6;i++)
    {
         U1TXREG = in1_buffer[i];               
         __delay32(1500); 
    }
    in1_buffer[0]= 0x00; 
    in1_buffer[in1_buffer[4]+5]= 0x00; 


}

